package com.yunxi.controller.sys;

import com.yunxi.core.web.*;
import com.yunxi.dealer.pojo.PartnerVO;
import com.yunxi.sys.pojo.StaffVO;
import com.yunxi.sys.service.StaffDubboService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 员工信息 前端控制器
 * </p>
 *
 * @author
 * @since 2019-05-20
 */
@RestController
@RequestMapping("/admin/staff")
@Api(tags = "pms:staff", description = "员工信息接口")
public class StaffController {
    private static final Logger logger = LoggerFactory.getLogger(StaffController.class);

    @Autowired
    StaffDubboService staffDubboService;

    @GetMapping("/page")
    @ApiOperation(value = "查询分页")
    public ApiResult page(StaffVO staffVO, Integer pageNum, Integer pageSize,@CurrentUser LoginInfo loginInfo) throws Exception {

        Object partner = loginInfo.getPartner();
        if(partner!=null){
            PartnerVO partnerVO = new PartnerVO();
            BeanUtils.copyProperties(partnerVO,partner);
            staffVO.setPartnerCode(partnerVO.getPartnerCode());
        }else {
            if(staffVO.getIsPartner()==null)
                staffVO.setIsPartner(0);
        }
       return ResponseUtil.ok(staffDubboService.findPageByVO(staffVO, pageNum, pageSize,loginInfo));
    }

   @PostMapping("/importExcel")
   @ApiOperation(value = "导入更新培训状态")
   public ApiResult list(@RequestBody List<StaffVO> staffVOList,@CurrentUser LoginInfo loginInfo) throws Exception {
      return staffDubboService.updateBatchTraining(staffVOList,loginInfo);
   }

    @GetMapping("/list")
    @ApiOperation(value = "查询列表")
    public ApiResult list(StaffVO staffVO,@CurrentUser LoginInfo loginInfo) throws Exception {
        return ResponseUtil.ok(staffDubboService.listByVO(staffVO,loginInfo));
    }

    @GetMapping("/load")
    @ApiOperation(value = "读取单个")
    public ApiResult load(@RequestParam String staffId, @CurrentUser LoginInfo loginInfo) throws Exception {
        return ResponseUtil.ok(staffDubboService.getCasById(staffId));
    }

    @GetMapping("/listRole")
    @ApiOperation(value = "读取角色列表")
    public ApiResult listRole(@CurrentUser LoginInfo loginInfo) throws Exception {
        return ResponseUtil.ok(staffDubboService.listRole(loginInfo));
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存信息")
    public ApiResult save(@RequestBody StaffVO staffVO, @CurrentUser LoginInfo loginInfo) throws Exception {
       if (StringUtils.isEmpty(staffVO.getPartnerCode())) {
          if (StringUtils.isEmpty(loginInfo.getPartnerCode())) {
             staffVO.setPartnerCode(loginInfo.getPartnerCode());
          }
       }
        staffDubboService.saveOrUpdate(staffVO,loginInfo);
        return ResponseUtil.ok("success");
    }


    @PostMapping("/saveRegionManager")
    @ApiOperation(value = "保存大区经理")
    public ApiResult save(@RequestBody List<StaffVO> staffVOList, @CurrentUser LoginInfo loginInfo) throws Exception {

        staffDubboService.saveOrUpdate(staffVOList,loginInfo);
        return ResponseUtil.ok("success");
    }

    @PostMapping("/saveCityManager")
    @ApiOperation(value = "保存小区经理")
    public ApiResult saveCityManager(@RequestBody List<StaffVO> staffVOList, @CurrentUser LoginInfo loginInfo) throws Exception {

        return staffDubboService.saveCityManager(staffVOList,loginInfo);
    }

    @GetMapping("/listCityManagerByAccountNo")
    @ApiOperation(value = "查询大区经理下的小区经理")
    public ApiResult listCityManagerByAccountNo(String accountNo,Integer pageNum, Integer pageSize) throws Exception {
        return ResponseUtil.ok(staffDubboService.listCityManagerByAccountNo(accountNo,pageNum,pageSize));
    }

    @PostMapping("/del")
    @ApiOperation(value = "删除")
    public ApiResult del(@RequestBody List<StaffVO> staffVOList) throws Exception {
        staffDubboService.removeByListVo(staffVOList);
        return ResponseUtil.ok("success");
    }

    @PostMapping("/batchCreateUser")
    @ApiOperation(value = "删除")
    public ApiResult batchCreateUser(@RequestBody String flag) throws Exception {
        staffDubboService.batchCreateUser(flag);
        return ResponseUtil.ok("success");
    }

    @GetMapping("/listSaler")
    @ApiOperation(value = "根据渠道code查询销售顾问")
    public ApiResult listSaler( String partnerCode ) throws Exception {
        return ResponseUtil.ok(staffDubboService.listByPartnerCode(partnerCode));
    }

    @GetMapping("/listManager")
    @ApiOperation(value = "根据渠道code查询销售经理")
    public ApiResult listManager( String partnerCode) throws Exception {
        return ResponseUtil.ok(staffDubboService.listManagerByPartnerCode(partnerCode));
    }

    @GetMapping("/listRegionManager")
    @ApiOperation(value = "根据归属地区代码查询大区经理")
    public ApiResult listRegionManager(@RequestParam String areaCode,Integer pageNum, Integer pageSize) throws Exception {
        return ResponseUtil.ok(staffDubboService.listRegionManager(areaCode, pageNum, pageSize));
    }

    @GetMapping("/listRegionManagerNotExits")
    @ApiOperation(value = "根据归属地区代码查询不存在该大区经理")
    public ApiResult listRegionManagerNotExits(@RequestParam String areaCode,Integer pageNum, Integer pageSize) throws Exception {
        return ResponseUtil.ok(staffDubboService.listRegionManagerNotExits(areaCode, pageNum, pageSize));
    }

    @GetMapping("/listAllRegionManager")
    @ApiOperation(value = "查询所有大区经理")
    public ApiResult listAllRegionManager(Integer pageNum, Integer pageSize) throws Exception {
        return ResponseUtil.ok(staffDubboService.listAllRegionManager(pageNum,pageSize));
    }

    @GetMapping("/listAllCityManager")
    @ApiOperation(value = "查询所有城市辅导经理(小区经理)")
    public ApiResult listAllCityManager(Integer pageNum, Integer pageSize) throws Exception {
        return ResponseUtil.ok(staffDubboService.listAllCityManager(pageNum,pageSize));
    }

    @GetMapping("/listCityManagerNotExits")
    @ApiOperation(value = "查询未配置的城市辅导经理(小区经理)")
    public ApiResult listCityManagerNotExits(Integer pageNum, Integer pageSize) throws Exception {
        return ResponseUtil.ok(staffDubboService.listCityManagerNotExits(pageNum,pageSize));
    }

    @GetMapping("/getBigRegionCode")
    @ApiOperation(value = "根据登陆账号查询大区code")
    public ApiResult getBigRegionCode(String userCode, @CurrentUser LoginInfo loginInfo) throws Exception {
        if ( DictConstant.USER_ROLE_CITYMANAGER.equals( loginInfo.getRoleCode())) return ResponseUtil.ok("roleCityManager");
        return ResponseUtil.ok(staffDubboService.getBigRegionCode(userCode));
    }

    @GetMapping("/listStaffByRoleCode")
    @ApiOperation(value = "根据角色代码查询当前登陆渠道商下的对应人员")
    public ApiResult listStaffByRoleCode(@RequestParam String roleCode, @CurrentUser LoginInfo loginInfo) throws Exception {
        return staffDubboService.listStaffByRoleCode(roleCode,loginInfo);
    }

    @GetMapping("/listRegionManagerByPartnerCode")
    @ApiOperation(value = "根据渠道商代码查询大区经理")
    public ApiResult listRegionManagerByPartnerCode(@RequestParam String partnerCode, @CurrentUser LoginInfo loginInfo) throws Exception {
        List<StaffVO> list = staffDubboService.listRegionManagerByPartnerCode(partnerCode,loginInfo);
        return ResponseUtil.ok(list);
    }

    @GetMapping("/listCityManagerByPartnerCode")
    @ApiOperation(value = "根据渠道商代码查询小区经理")
    public ApiResult listCityManagerByPartnerCode(@RequestParam String partnerCode, @CurrentUser LoginInfo loginInfo) throws Exception {
        List<StaffVO> list = staffDubboService.listCityManagerByPartnerCode(partnerCode,loginInfo);
        return ResponseUtil.ok(list);    }

    @GetMapping("/a")
    @ApiOperation(value = "a")
    public ApiResult a(@RequestParam String provinceCode) throws Exception {
        return staffDubboService.a(provinceCode);
    }
/*@PostMapping("/export")
@ApiOperation(value = "导出excel")
public Object export(@RequestParam Map<String,Object> params) throws Exception {
return ResponseUtil.ok(staffDubboService.findMapByList(params));
}*/
}
